/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.asu.web.ui.work;

import com.haulmont.asu.entity.Work;
import com.haulmont.cuba.gui.components.AbstractEditor;

import java.util.Date;

/**
 * Author: Sergey42
 * Date: 17.10.13 19:49
 */
public class WorkEditor extends AbstractEditor<Work> {

    @Override
    public void commitAndClose() {
        Work work = getItem();

        if (!checkOrder(work.getPlanStartDate(), work.getPlanFinishDate())) {
            showNotification(getMessage("planDateError"), NotificationType.WARNING);
            return;
        }

        if (!checkOrder(work.getFactStartDate(), work.getFactFinishDate())) {
            showNotification(getMessage("factDateError"), NotificationType.WARNING);
            return;
        }

        super.commitAndClose();
    }

    protected boolean checkOrder(Date start, Date finish) {
        if (start != null && finish != null) {
            return finish.after(start);
        }
        return true;
    }
}
